package com.jala.wx;

import com.alibaba.fastjson.JSON;
import com.jala.base.BaseAction;
import com.jala.base.Global;
import com.jala.cm.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/20.
 */
@Controller
@RequestMapping("/wx")
public class WxAction  extends BaseAction {

    @Autowired
    private Global global;

    @RequestMapping(value = "/login.htm",method = RequestMethod.GET)
    public String dealerLoginHtm(HttpServletRequest request, ModelMap model, HttpServletResponse response){
        String openId = request.getParameter("openId");
        logger.info("openId={}",openId);
        model.put("qrCodeMsg",openId);
        return "/wx/login";
    }

    @ResponseBody
    @RequestMapping(value = "/login.htm",method = RequestMethod.POST)
    public Result dealerLogin(HttpServletRequest request, HttpServletResponse response){
        Result result=new Result();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(StringUtils.isBlank(username)){
            result.setCode(-1);
            return result;
        }
        if(StringUtils.isBlank(password)){
            result.setCode(-1);
            return result;
        }
        global.setCacheValue(username, password);
        result.setCode(0);
        return result;
    }

    @RequestMapping(value = "/index.htm",method = RequestMethod.GET)
    public String indexHtm(HttpServletRequest request, ModelMap model, HttpServletResponse response){
        HttpSession session = request.getSession();
        logger.info("session={}", JSON.toJSONString(session));
        model.put("qrCodeMsg",JSON.toJSONString(session));
        Map<String,Object> map = new HashMap<>();
        return "/wx/index";
    }

}
